package org.zh.util.log;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class CustomFormatter extends Formatter {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    @Override
    public String format(LogRecord record) {
        StringBuilder sb = new StringBuilder();
        sb.append(record.getMillis()).append(" | ");
        sb.append("线程[").append(Thread.currentThread().getName()).append("] | ");
        sb.append("级别[").append(record.getLevel().getName()).append("] | ");
        sb.append("类[").append(record.getSourceClassName()).append("] | ");
        sb.append("方法[").append(record.getSourceMethodName()).append("] | ");
        sb.append(formatMessage(record)).append("\n");

        // 处理异常堆栈
        if (record.getThrown() != null) {
            try (java.io.StringWriter sw = new java.io.StringWriter();
                 java.io.PrintWriter pw = new java.io.PrintWriter(sw)) {
                record.getThrown().printStackTrace(pw);
                sb.append(sw);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sb.toString();
    }
}